package org.lee.status.app.activity;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * ActivityService
 * 活动服务
 *
 * @author dralee
 * @date 2021-11-24 09:45:00
 **/
public class ActivityService {

    private static Map<String, Status> statusMap = new ConcurrentHashMap<>();
    private static Map<String, ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();

    public static void init(String activityId, Status status) {
        ActivityInfo activityInfo = ActivityInfo.builder()
                .activityId(activityId).activityName("上班打卡领券活动")
                .status(status).beginTime(new Date()).endTime(Calendar.getInstance().getTime()).build();
        statusMap.put(activityId, status);
        activityInfoMap.put(activityId, activityInfo);
    }

    /**
     * 查询活动信息
     *
     * @param activityId: 活动Id
     * @return org.lee.status.app.activity.ActivityInfo
     * @author dralee
     * @date 2021/11/24 9:52
     **/
    public static ActivityInfo queryActivity(String activityId) {
        return activityInfoMap.get(activityId);
    }

    /**
     * 查询状态
     *
     * @param activityId:
     * @return org.lee.status.app.activity.Status
     * @author dralee
     * @date 2021/11/24 9:54
     **/
    public static Status queryActivityStatus(String activityId) {
        return statusMap.get(activityId);
    }

    public static synchronized void execStatus(String activityId, Status beforeStatus, Status afterStatus) {
        if (!beforeStatus.equals(statusMap.get(activityId))) {
            return;
        }
        statusMap.put(activityId, afterStatus);
        activityInfoMap.get(activityId).setStatus(afterStatus);
    }
}
